1
|
|
|
/*jslint |
2
|
|
|
regexp: true |
3
|
|
|
indent: 4 |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
/*global |
7
|
|
|
$, |
8
|
|
|
Line, Markers, Storage |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
var Lines = {}; |
12
|
|
|
Lines.m_map = null; |
13
|
|
|
Lines.m_nextLineId = 0; |
14
|
|
|
Lines.m_lines = []; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
Lines.init = function (themap) { |
18
|
|
|
'use strict'; |
19
|
|
|
|
20
|
|
|
Lines.m_map = themap; |
21
|
|
|
Lines.m_nextLineId = 0; |
22
|
|
|
Lines.m_lines = []; |
23
|
|
|
}; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
Lines.newLine = function (source, target) { |
27
|
|
|
'use strict'; |
28
|
|
|
|
29
|
|
|
var m1 = Markers.getById(source), |
30
|
|
|
m2 = Markers.getById(target); |
31
|
|
|
|
32
|
|
|
if (!m1 || m1.isFree()) { |
33
|
|
|
source = -1; |
34
|
|
|
} |
35
|
|
|
if (!m2 || m2.isFree()) { |
36
|
|
|
target = -1; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
this.m_lines.push(new Line(Lines.m_map, this.m_nextLineId, source, target)); |
40
|
|
|
this.m_nextLineId += 1; |
41
|
|
|
this.updateTotalDistance(); |
42
|
|
|
this.store(); |
43
|
|
|
}; |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
Lines.getLineIndex = function (id) { |
47
|
|
|
'use strict'; |
48
|
|
|
|
49
|
|
|
var index, line; |
50
|
|
|
|
51
|
|
|
for (index = 0; index < this.m_lines.length; index += 1) { |
52
|
|
|
line = this.m_lines[index]; |
53
|
|
|
if (line && line.getId() === id) { |
54
|
|
|
return index; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return -1; |
59
|
|
|
}; |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
Lines.getLineById = function (id) { |
63
|
|
|
'use strict'; |
64
|
|
|
|
65
|
|
|
var index = this.getLineIndex(id); |
66
|
|
|
if (index < 0) { |
67
|
|
|
return null; |
68
|
|
|
} |
69
|
|
|
return this.m_lines[index]; |
70
|
|
|
}; |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
Lines.getLinesText = function () { |
74
|
|
|
'use strict'; |
75
|
|
|
|
76
|
|
|
var a = []; |
77
|
|
|
this.m_lines.map(function (line) { |
78
|
|
|
if (line) { |
79
|
|
|
a.push(line.getEndpointsString()); |
80
|
|
|
} |
81
|
|
|
}); |
82
|
|
|
return a.join("*"); |
83
|
|
|
}; |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
Lines.store = function () { |
87
|
|
|
'use strict'; |
88
|
|
|
|
89
|
|
|
Storage.set("lines", this.getLinesText()); |
90
|
|
|
}; |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
Lines.selectLineSourceById = function (id, markerId) { |
94
|
|
|
'use strict'; |
95
|
|
|
|
96
|
|
|
this.getLineById(id).setSource(markerId); |
97
|
|
|
this.updateTotalDistance(); |
98
|
|
|
this.store(); |
99
|
|
|
}; |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
Lines.selectLineSource = function (id) { |
103
|
|
|
'use strict'; |
104
|
|
|
|
105
|
|
|
var markerId = -1, |
106
|
|
|
opt = $("#dynline" + id + " .source option:selected"); |
107
|
|
|
|
108
|
|
|
if (opt) { |
109
|
|
|
markerId = parseInt(opt.val(), 10); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
this.selectLineSourceById(id, markerId); |
113
|
|
|
}; |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
Lines.selectLineTargetById = function (id, markerId) { |
117
|
|
|
'use strict'; |
118
|
|
|
|
119
|
|
|
this.getLineById(id).setTarget(markerId); |
120
|
|
|
this.updateTotalDistance(); |
121
|
|
|
this.store(); |
122
|
|
|
}; |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
Lines.selectLineTarget = function (id) { |
126
|
|
|
'use strict'; |
127
|
|
|
|
128
|
|
|
var markerId = -1, |
129
|
|
|
opt = $("#dynline" + id + " .target option:selected"); |
130
|
|
|
|
131
|
|
|
if (opt) { |
132
|
|
|
markerId = parseInt(opt.val(), 10); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
this.selectLineTargetById(id, markerId); |
136
|
|
|
}; |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
Lines.updateLinesMarkerMoved = function (markerId) { |
140
|
|
|
'use strict'; |
141
|
|
|
|
142
|
|
|
this.m_lines.map(function (line) { |
143
|
|
|
if (line) { |
144
|
|
|
line.updateMarkerMoved(markerId); |
145
|
|
|
} |
146
|
|
|
}); |
147
|
|
|
|
148
|
|
|
this.updateTotalDistance(); |
149
|
|
|
}; |
150
|
|
|
|
151
|
|
|
|
152
|
|
|
Lines.updateLinesMarkerAdded = function () { |
153
|
|
|
'use strict'; |
154
|
|
|
|
155
|
|
|
this.m_lines.map(function (line) { |
156
|
|
|
if (line) { |
157
|
|
|
line.updateMarkerAdded(); |
158
|
|
|
} |
159
|
|
|
}); |
160
|
|
|
}; |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
Lines.updateLinesMarkerRemoved = function (markerId) { |
164
|
|
|
'use strict'; |
165
|
|
|
|
166
|
|
|
this.m_lines.map(function (line) { |
167
|
|
|
if (line) { |
168
|
|
|
line.updateMarkerRemoved(markerId); |
169
|
|
|
} |
170
|
|
|
}); |
171
|
|
|
|
172
|
|
|
this.updateTotalDistance(); |
173
|
|
|
this.store(); |
174
|
|
|
}; |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
Lines.updateLine = function (id) { |
178
|
|
|
'use strict'; |
179
|
|
|
|
180
|
|
|
var index = this.getLineIndex(id); |
181
|
|
|
if (index < 0) { |
182
|
|
|
return; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
this.m_lines[index].update(); |
186
|
|
|
this.updateTotalDistance(); |
187
|
|
|
}; |
188
|
|
|
|
189
|
|
|
|
190
|
|
|
Lines.deleteLine = function (id) { |
191
|
|
|
'use strict'; |
192
|
|
|
|
193
|
|
|
$('#dynline' + id).remove(); |
194
|
|
|
|
195
|
|
|
var index = this.getLineIndex(id); |
196
|
|
|
if (index < 0 || !this.m_lines[index]) { |
197
|
|
|
return; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
this.m_lines[index].clearMapObject(); |
201
|
|
|
this.m_lines[index] = null; |
202
|
|
|
|
203
|
|
|
this.updateTotalDistance(); |
204
|
|
|
this.store(); |
205
|
|
|
}; |
206
|
|
|
|
207
|
|
|
|
208
|
|
|
Lines.deleteAllLines = function () { |
209
|
|
|
'use strict'; |
210
|
|
|
|
211
|
|
|
var self = this; |
212
|
|
|
this.m_lines.map(function (line) { |
213
|
|
|
if (line) { |
214
|
|
|
self.deleteLine(line.getId()); |
215
|
|
|
} |
216
|
|
|
}); |
217
|
|
|
|
218
|
|
|
this.updateTotalDistance(); |
219
|
|
|
}; |
220
|
|
|
|
221
|
|
|
|
222
|
|
|
Lines.updateTotalDistance = function () { |
223
|
|
|
'use strict'; |
224
|
|
|
|
225
|
|
|
var lines = 0; |
226
|
|
|
var distance = 0; |
227
|
|
|
|
228
|
|
|
this.m_lines.map(function (line) { |
229
|
|
|
if (line) { |
230
|
|
|
var d = line.distance(); |
231
|
|
|
lines += 1; |
232
|
|
|
if (d > 0) { |
233
|
|
|
distance += d; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
}); |
237
|
|
|
|
238
|
|
|
if (lines > 0) { |
239
|
|
|
$('#lineDist').html(distance.toFixed() + "m"); |
240
|
|
|
$('#lineDistDiv').show(); |
241
|
|
|
} else { |
242
|
|
|
$('#lineDistDiv').hide(); |
243
|
|
|
} |
244
|
|
|
}; |
245
|
|
|
|